import { defineStore } from "pinia";
import { BASEHOME_NAME, BASEHOE, BASEHOME_TITLE } from "@/config.js";

const formatTag = (route) => {
  const { name, meta, fullPath, query, params } = route;

  return {
    title: meta.breadcrumb ? meta.breadcrumb(route) : meta.title || "",
    name: String(name),
    fullPath,
    query,
    params,
    ignore: meta.ignore
  };
};

const DEFAULT_ROUTE = {
  title: BASEHOME_TITLE,
  name: BASEHOME_NAME,
  fullPath: BASEHOE
};
export const useTabBarStore = defineStore("tab-bar", {
  state: () => ({
    tagList: [DEFAULT_ROUTE]
  }),
  getters: {
    getTabList() {
      return this.tagList;
    }
  },

  actions: {
    updateTabList(route = {}) {
      const { name, meta } = route;
      if (["NotFoundPage"].includes(name)) return;
      if (meta.ignore) return;
      if (!this.tagList.some((tag) => tag.fullPath === route.fullPath)) {
        this.tagList.push(formatTag(route));
      }

      // if (!route.meta.ignoreCache) {
      //   this.cacheTabList.add(route.name);
      // }
    },
    freshTabList(tags) {
      const result = tags.filter((item) => {
        return !item.ignore;
      });
      this.tagList = result;
    },
    deleteTag(idx) {
      this.tagList.splice(idx, 1);
    },

    resetTabList() {
      this.tagList = [DEFAULT_ROUTE];
    }
  }
});
